package com.topscomm.gpm.script;

import com.topscomm.cbo.pojo.CboApprovalNodeInsEntity;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.SpringUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.gpm.GpmScript;
import com.topscomm.gpm.GpmSystemConst;
import com.topscomm.gpm.pojo.GpmProjectAppointmentEntity;
import com.topscomm.gpm.pojo.GpmProjectEntity;
import com.topscomm.gpm.service.impl.GpmProjectAppointmentService;
import com.topscomm.gpm.service.impl.GpmProjectService;
import com.topscomm.gpm.service.impl.GpmProjectTeamService;
import com.topscomm.tap.ITapService;
import com.topscomm.tap.TapEntity;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Classname SdAppointmentSendEmailScript
 * @Date 2021/12/16
 * @Created by jiangtao
 */
public class GpmAppointmentSendEmailScript extends GpmScript {
	@Transactional
	public void sendEmail(ITapService service, TapEntity entity) throws Exception {
		// 审批节点实例
		CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
		String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
		GpmProjectAppointmentService gpmProjectAppointmentService = (GpmProjectAppointmentService) SpringUtil.getBean("gpmProjectAppointmentService");

		// 获取任命书信息
		GpmProjectAppointmentEntity gpmProjectAppointmentEntity = gpmProjectAppointmentService.queryById(sourceid);
		if (gpmProjectAppointmentService.judgeSendTime(null, gpmProjectAppointmentEntity.getCreateon())) {
			return;
		}
		// 重新生成任命书
		gpmProjectAppointmentService.sendMessage(ConvertUtil.convertToString(gpmProjectAppointmentEntity.getId()));
	}

	@Transactional
	public void projectAppointmentSendMessage(ITapService service, TapEntity entity) throws Exception {
		// 审批点实例
		CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
		String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
		String sourcetype = ConvertUtil.convertToString(nodeins.getSourcetype());
		GpmProjectTeamService gpmProjectTeamService = (GpmProjectTeamService) SpringUtil.getBean("gpmProjectTeamService");

		GpmProjectAppointmentService gpmProjectAppointmentService = (GpmProjectAppointmentService) SpringUtil.getBean("gpmProjectAppointmentService");
		// 获取任命书信息
		GpmProjectAppointmentEntity gpmProjectAppointmentEntity = gpmProjectAppointmentService.queryById(sourceid);
		// 查询项目编号
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle("projectcode = '{0}'", gpmProjectAppointmentEntity.getProjectcode());
		// 查询项目是否为第一次任命
		int count = gpmProjectAppointmentService.getCount(sql.toString());
		// 查询部门是否发送任命
		boolean appointmentSend = gpmProjectAppointmentService.queryAppointmentSendByDept(gpmProjectAppointmentEntity.getDeptcommoncode1());
		if (count > 1 && appointmentSend) {
			// 消息内容
			String contentInfo = "任命";
			// 给项目组成员发送消息
			gpmProjectTeamService.sendMessageToGroupMember(sql.toString(), sourceid, sourcetype, contentInfo);
		}
	}

	@Transactional
	public void SendAppMessageToCDProjectGroupMember(ITapService service, TapEntity entity) throws Exception {
		// 审批点实例
		CboApprovalNodeInsEntity nodeins = (CboApprovalNodeInsEntity) entity;
		String sourceid = ConvertUtil.convertToString(nodeins.getSourceid());
		String sourcetype = ConvertUtil.convertToString(nodeins.getSourcetype());
		GpmProjectService gpmProjectService = (GpmProjectService) SpringUtil.getBean("gpmProjectService");
		GpmProjectTeamService gpmProjectTeamService = (GpmProjectTeamService) SpringUtil.getBean("gpmProjectTeamService");
		GpmProjectAppointmentService gpmProjectAppointmentService = (GpmProjectAppointmentService) SpringUtil.getBean("gpmProjectAppointmentService");
		// 获取任命书信息
		GpmProjectAppointmentEntity gpmProjectAppointmentEntity = gpmProjectAppointmentService.queryById(sourceid);
		// 查询项目编号
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle("projectcode = '{0}'", gpmProjectAppointmentEntity.getProjectcode());
		// 只给C、D级项目组成员发送App消息
		GpmProjectEntity gpmProjectEntity = gpmProjectService.queryByWhere(sql.toString()).get(0);
		String sdId = ConvertUtil.convertToString(gpmProjectEntity.getId());
		List<String> levelList = new ArrayList<String>();
		levelList.add(GpmSystemConst.SdProjectLevel.C);
		levelList.add(GpmSystemConst.SdProjectLevel.D);
		// 查询部门是否发送任命
		boolean appointmentSend = gpmProjectAppointmentService.queryAppointmentSendByDept(gpmProjectAppointmentEntity.getDeptcommoncode1());
		if (levelList.contains(gpmProjectEntity.getProjectlevel()) && appointmentSend) {
			gpmProjectTeamService.sendAppMessageToGroupMember(sql.toString(), sdId, sourcetype);
		}
	}
}
